package m4;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class Producer {
    public static void main(String[] args) throws Exception{
        /*连接*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.64.140");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        /*创建direct类型交换机 : direct_logs*/
        channel.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);

        /*发送消息, 消息上需要携带路由关键词*/
        while (true) {
            System.out.print("输入消息:");
            String s = new Scanner(System.in).nextLine();
            System.out.print("输入关键词:");
            String k = new Scanner(System.in).nextLine();
            /*对于默认交换机, 使用队列名作为路由键*/
            channel.basicPublish("direct_logs", k, null, s.getBytes());
        }
    }
}
